package istar.impl;


import istar.IActor;
import istar.IElement;
import istar.IElementLink;
import istar.ITask;

import java.util.Iterator;
import java.util.Vector;


/**
 * @author kinder
 * @model
 */
public class MeansEndLink extends AbstractElementLink implements IElementLink {

	
	public MeansEndLink(String name,  IElement _end, ITask _mean){
		super(name, _end, _mean);
		_end.setMeansEndLink(this);
		_mean.getMeansFor().add(this);
	}
	
	protected MeansEndLink(String name){
		super(name);
	}
	
	protected MeansEndLink(){
		super();
	}

	public Object clone(IActor newOwner) throws CloneNotSupportedException {
		MeansEndLink meansEndClone = (MeansEndLink)super.clone();
		meansEndClone.setName(this.getName());
		
	
		meansEndClone.setType(this.getType());
		meansEndClone.setTargetElements(new Vector<IElement>());

		Iterator<IElement> itSubElmnts = this.getTargetElements().iterator();
		
		while (itSubElmnts.hasNext()){
		
			AbstractElement subElmnt = (AbstractElement)itSubElmnts.next();
			if (subElmnt.getOwner()==this.getOwner().getOwner()){
				AbstractElement subClone = (AbstractElement)subElmnt.clone(newOwner);

				meansEndClone.getTargetElements().add(subClone);
				((Task)subClone).getMeansFor().add(meansEndClone);
			}
			else {
				meansEndClone.getTargetElements().add(subElmnt);
			}
		}
		
		return meansEndClone;
	}



	
}
